﻿/**
 * MOTOR 2 - RAPID FLASH DYNAMICS
 * Copyright (c) 2006-2008 Michael Baczynski http://www.polygonal.de
 * 
 * Based on Box2D by Erin Catto, http://www.box2d.org
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 * 
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */
package de.polygonal.motor2.dynamics.contact
{
	public class ContactID
	{
		public var refFace:int, incEdge:int, incVert:int, flip:int;
		
		public static var NULL_FEATURE:int = 1 << 16;
		
		public static const NO_EDGE:int = 0;
		
		public var inEdge1:int, outEdge1:int;
		public var inEdge2:int, outEdge2:int;
		
		public function copy():ContactID
		{
			var id:ContactID = new ContactID();
			id.refFace = refFace;
			id.incEdge = incEdge;
			id.incVert = incVert;
			id.flip    = flip;
			return id;
		}
		
		public function zero():void
		{
			refFace = incEdge = incVert = flip = 0;
		}
		
		public function get key():int
		{
			return refFace << 24 | incEdge << 16 | incVert << 8 | flip;
			
			
//			return (refFace <q< 24) + (incEdge << 16) + (incVert << 8) + flip;
			
			//return inEdge1 << 24 | inEdge2 << 16 | inEdge3 << 8 | inEdge4;
			
			//return inEdge1 + (outEdge1 << 4) + (inEdge2 << 8) + (outEdge2 << 12);
			
			/*
			 * var referenceFace:int  = 4;
			var incidendEdge:int   = 2;
			var incidentVertex:int = 9;
			var flip:int           = 1;
			var features:int = (referenceFace << 24) | (incidendEdge << 16) | (incidentVertex << 8) | flip;
			
			trace(features);*/
		}
		
		public function ContactID(source:ContactID = null)
		{
			if (source)
			{
				inEdge1 = source.inEdge1;
				inEdge2 = source.inEdge2;
				
				outEdge1 = source.outEdge1;
				outEdge2 = source.outEdge2;
			}
			else
			{
				inEdge1 = outEdge1 = inEdge2 = outEdge2 = NO_EDGE;
			}
		}
		
		public function init():void
		{
			inEdge1 = outEdge1 = inEdge2 = outEdge2 = NO_EDGE;
		}
		
		public function flipme():void
		{
			inEdge1 ^= inEdge2;
			inEdge2 ^= inEdge1;
			inEdge1 ^= inEdge2;
			
			outEdge1 ^= outEdge2;
			outEdge2 ^= outEdge1;
			outEdge1 ^= outEdge2;
		}
		
		//public function copy():FeaturePair
		//{
			//var f:FeaturePair = new FeaturePair();
			//f.inEdge1 = f.inEdge1;
			//f.inEdge2 = f.inEdge2;
			//
			//f.outEdge1 = f.outEdge1;
			//f.outEdge2 = f.outEdge2;
			//
			//return f;
		//}
		//
		//public function paste(f:FeaturePair):void
		//{
			//inEdge1  = f.inEdge1;
			//inEdge2  = f.inEdge2;
			//
			//outEdge1 = f.outEdge1;
			//outEdge2 = f.outEdge2;
		//}
	}
}
